import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.*;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import java.text.*;

public class Rec extends JPanel implements KeyListener, ActionListener{
	
	
	
	//class constants
	private static final int WINDOW_WIDTH = 955;
	private static final int WINDOW_HEIGHT =500;
	
	//text field
	public JTextArea typeThis = new JTextArea();
	private JTextField textTest = new JTextField();
	private JTextField resultText = new JTextField();
	
	//reset button
	public JButton reset = new JButton("RESET");
	//QUIT BUTTON BACK TO MAIN MENU
	public JButton mainMenu = new JButton("QUIT");
	
	
	public Gen textGen = new Gen();
	
	
	
	Highlighter yellowHighlighter = typeThis.getHighlighter();
	Highlighter.HighlightPainter painter;
	
	public int initial =0;
	public int start=0;
	public int error=0;
	
	//CONSIDER MAKING A USER CLASS TO STORE PHRASE COUNTS AND THEIR WPM
	
	public int phraseCount = 1; //this variable is used to average the users wpm, it can be 1 or 2 (2 once the user has typed the first phrase)
	public float userWPM=0;
	
	public Font typed = new Font(typeThis.getFont().getName(),Font.ITALIC,30);
	
	
	public Timer stopwatch = new Timer();
	
	
	//WPM 
	public String WPM(){
		float wpm = 0;
		if(stopwatch.timeElapsed() > 0)
			wpm = textGen.getWordCount(textGen.getPhrase())/(float)stopwatch.timeElapsed();
		userWPM = (userWPM+wpm)/phraseCount;
		DecimalFormat myFormatter = new DecimalFormat("###.##");
		return myFormatter.format(userWPM);
		
	}
	
	public Rec(){
		//newest additions
		typeThis.setLineWrap(true);
		typeThis.setWrapStyleWord(true);
		
	
		//end
		BorderLayout flow = new BorderLayout();
		this.setLayout(flow);
		flow.setHgap(20);
		flow.setVgap(20);
		
		clickListener c = new clickListener();
		
		reset.addActionListener(c);
		
		typeThis.addKeyListener(this);
		this.add(typeThis, BorderLayout.CENTER);
		this.add(resultText, BorderLayout.NORTH);
		Container container = new Container();
		container.setLayout(new FlowLayout());
		container.add(reset);
		container.add(mainMenu);
		this.add(container, BorderLayout.EAST);
		
		
		
		//font
				Font largeFont = new Font(typeThis.getFont().getName(),Font.BOLD,40);
				typeThis.setFont(largeFont);
				typeThis.setEditable(false);
				resultText.setEditable(false);
		
		
		
		
		this.setVisible(true);
		typeThis.requestFocus();
		
		typeThis.setText(textGen.getPhrase());
	}
	
	
	private class clickListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(e.getSource() == reset){
				start =0;
				initial=0;
				error=0;
				resultText.setText("START");
				
				typeThis.requestFocus();
				yellowHighlighter.removeAllHighlights();
				try {
					textGen.resetPhrase();
					typeThis.setText(textGen.getPhrase());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
				
			
		}
	}
	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
		
	}
	
	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
		typeThis.requestFocus();
		
		char answer = arg0.getKeyChar();
		String ans = Character.toString(answer);
		
		if(start == 0){
			stopwatch.startTime();
			start++;
		}
		
		char compare = textGen.getPhrase().charAt(initial);
		if(compare == answer){
			initial++;
			try {
				yellowHighlighter.addHighlight(0,initial,DefaultHighlighter.DefaultPainter);
			} catch (BadLocationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
				error++;
				
			}
		resultText.setText(ans);
		
			if(initial == textGen.getLength()){
				stopwatch.endTime();
				phraseCount++;
				resultText.setText("Time: " + String.valueOf(stopwatch.timeElapsed())+ " mins. You made :"+ error+ " mistakes   (avg) WPM:"+ WPM());
				
				try {
					textGen.resetPhrase();
					typeThis.setText(textGen.getPhrase());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				start =0;
				initial=0;
				error=0;
				
				typeThis.requestFocus();
			
				yellowHighlighter.removeAllHighlights();
			}
		}
		
	
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}
}
